package com.example.springboot011problemdetail.controller;

import com.example.springboot011problemdetail.Exception.BookNotFoundException;
import com.example.springboot011problemdetail.Exception.IsbnNotFoundException;
import com.example.springboot011problemdetail.pojo.Book;
import com.example.springboot011problemdetail.pojo.BookContainer;
import jakarta.annotation.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

/**
 * @description: TODO
 * @author: uu
 * @data: 2023/7/8 下午 02:42
 * @version: v1.0
 */
@RestController
public class BookController {
    @Resource
    private BookContainer bookContainer;

    @GetMapping("/book")
    Book getBook(String isbn) throws Exception{
        Optional<Book> book = bookContainer.getBooks().stream().filter(el->el.isbn().equals(isbn)).findFirst();
        if (book.isEmpty()){
//            throw new BookNotFoundException("isbn" + isbn + "-> 没有此图书");
            throw new IsbnNotFoundException(HttpStatus.NOT_FOUND,"isbn:" + isbn + "-> 没有此图书" );
        }
        return book.get();
    }
}
